package com.lyyzoo.util;

import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.StandardToStringStyle;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;

public abstract class Strings extends StringUtils {
    public static final String EMPTY = "";
    public static final String SPACE = " ";
    public static final int INDEX_NOT_FOUND = -1;
    public static final char SEPARATOR = '_';
    public static final String ALPHANUMERIC = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

    public Strings() {
    }

    public static String newString(String str, String charset) {
        return newString(str, Charsets.UTF_8.toString(), charset);
    }

    public static String newString(String str, Charset charset) {
        return newString(str, Charsets.UTF_8, charset);
    }

    public static String newString(String str, String decode_charset, String charset) {
        return newString(str, decode_charset, charset);
    }

    public static String newString(String str, Charset decode_charset, Charset charset) {
        return new String(str.getBytes(decode_charset), charset);
    }

    public static String newStringFromUnicode(String str) {
        int start = 0;
//        int end = false;

        StringBuffer buffer;
        int end;
        for(buffer = new StringBuffer(); start > -1; start = end) {
            end = str.indexOf("\\u", start + 2);
            String charStr = "";
            if (end == -1) {
                charStr = str.substring(start + 2, str.length());
            } else {
                charStr = str.substring(start + 2, end);
            }

            char letter = (char)Integer.parseInt(charStr, 16);
            buffer.append((new Character(letter)).toString());
        }

        return buffer.toString();
    }

    public static boolean isNullOrEmpty(String str) {
        return com.google.common.base.Strings.isNullOrEmpty(str);
    }

    public static boolean isNullOrEmpty(Object... objs) {
        Object[] var1 = objs;
        int var2 = objs.length;

        for(int var3 = 0; var3 < var2; ++var3) {
            Object obj = var1[var3];
            String str;
            if (obj instanceof String) {
                str = (String)obj;
            } else {
                str = Casts.toString(obj);
            }

            if (isEmpty(str) || isBlank(str)) {
                return true;
            }
        }

        return false;
    }

    public static boolean isNotNullOrEmpty(Object... objs) {
        boolean result = true;
        if (objs != null && objs.length != 0) {
            Object[] var2 = objs;
            int var3 = objs.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                Object obj = var2[var4];
                String value;
                if (obj instanceof String) {
                    value = (String)obj;
                } else {
                    value = Casts.toString(obj);
                }

                result &= null != value && !"".equals(value);
            }
        } else {
            result = false;
        }

        return result;
    }

    public static boolean isEmpty(CharSequence... css) {
        return StringUtils.isAnyEmpty(css);
    }

    public static boolean isInteger(String str) {
        int begin = 0;
        if (str != null && !str.trim().equals("")) {
            str = str.trim();
            if (str.startsWith("+") || str.startsWith("-")) {
                if (str.length() == 1) {
                    return false;
                }

                begin = 1;
            }

            for(int i = begin; i < str.length(); ++i) {
                if (!Character.isDigit(str.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return false;
        }
    }

    public static boolean isEquals(CharSequence cs1, CharSequence cs2) {
        return StringUtils.equals(cs1, cs2);
    }

    public static boolean isNotEquals(CharSequence cs1, CharSequence cs2) {
        return !StringUtils.equals(cs1, cs2);
    }

    public static boolean isEqualsIgnoreCase(CharSequence str1, CharSequence str2) {
        return StringUtils.equalsIgnoreCase(str1, str2);
    }

    public static boolean isNotEqualsIgnoreCase(CharSequence str1, CharSequence str2) {
        return !isEqualsIgnoreCase(str1, str2);
    }

    public static boolean isEquals(String p1, String p2, String separator) {
        p1 = StringUtils.isBlank(p1) ? "" : p1;
        p2 = StringUtils.isBlank(p1) ? "" : p2;
        String[] paramsArray1 = p1.split(separator);
        String[] paramsArray2 = p2.split(separator);
        return Arrays.equals(paramsArray1, paramsArray2);
    }

    public static boolean isContains(String params, String param, String separator) {
        return ArrayUtils.contains(params.split(separator), param);
    }

    public static boolean isContains(CharSequence seq, CharSequence searchSeq) {
        return StringUtils.contains(seq, searchSeq);
    }

    public static boolean isContainsIgnoreCase(CharSequence str, CharSequence searchStr) {
        return StringUtils.containsIgnoreCase(str, searchStr);
    }

    public static boolean isUrl(String str) {
        return Regexs.isUrl(str);
    }

    public static boolean hasLength(CharSequence str) {
        return str != null && str.length() > 0;
    }

    public static boolean hasLength(String str) {
        return hasLength((CharSequence)str);
    }

    public static boolean hasText(CharSequence str) {
        if (!hasLength(str)) {
            return false;
        } else {
            int strLen = str.length();

            for(int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(str.charAt(i))) {
                    return true;
                }
            }

            return false;
        }
    }

    public static boolean hasText(String str) {
        return hasText((CharSequence)str);
    }

    public static String capitalize(String str) {
        return changeFirstCharacterCase(str, true);
    }

    public static String uncapitalize(String str) {
        return changeFirstCharacterCase(str, false);
    }

    public static String remove(String str, String... removes) {
        String result = new String(str);
        String[] var3 = removes;
        int var4 = removes.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            String remove = var3[var5];
            result = remove(result, remove);
        }

        return result;
    }

    public static String removeNT(String str) {
        return remove(str, "\n", "\t", "\r");
    }

    public static String removeWhitespace(String str) {
        return str.replaceAll("\\s+", "");
    }

    public static String substringAfterIgnoreCases(String str, String separator) {
        if (isEmpty(str)) {
            return str;
        } else if (separator == null) {
            return "";
        } else {
            int pos = indexOfIgnoreCase(str, separator);
            return pos == -1 ? "" : str.substring(pos + separator.length());
        }
    }

    public static String substringAfterIgnoreCases(String str, String... separators) {
        String[] var2 = separators;
        int var3 = separators.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            String separator = var2[var4];
            str = substringAfterIgnoreCases(str, separator);
        }

        return str;
    }

    public static String substringAfterAndContainsIgnoreCases(String str, String separator) {
        return StringUtils.substring(str, indexOfIgnoreCase(str, separator));
    }

    public static String substringAfterAndContainsIgnoreCases(String str, String... separators) {
        String[] var2 = separators;
        int var3 = separators.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            String separator = var2[var4];
            str = substringAfterAndContainsIgnoreCases(str, separator);
        }

        return str;
    }

    public static String substringBeforeIgnoreCases(String str, String separator) {
        if (!isEmpty(str) && separator != null) {
            if (separator.isEmpty()) {
                return "";
            } else {
                int pos = indexOfIgnoreCase(str, separator);
                return pos == -1 ? str : str.substring(0, pos);
            }
        } else {
            return str;
        }
    }

    public static String substringBeforeIgnoreCases(String str, String... separators) {
        String[] var2 = separators;
        int var3 = separators.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            String separator = var2[var4];
            str = substringBeforeIgnoreCases(str, separator);
        }

        return str;
    }

    public static String substringBeforeFirstSeparator(String str, String separator) {
        if (!isEmpty(str) && separator != null) {
            if (separator.isEmpty()) {
                return "";
            } else {
                int pos = indexOf(str, separator);
                return pos == -1 ? str : str.substring(0, pos);
            }
        } else {
            return str;
        }
    }

    public static String substringBeforeLastSeparator(String str, String separator) {
        if (!isEmpty(str) && separator != null) {
            if (separator.isEmpty()) {
                return "";
            } else {
                int pos = lastIndexOf(str, separator);
                return pos == -1 ? str : str.substring(0, pos);
            }
        } else {
            return str;
        }
    }

    public static void println(Object... objs) {
        StringBuffer sb = new StringBuffer();

        for(int i = 0; i < objs.length; ++i) {
            Object obj = objs[i];
            sb.append(toString(obj) + "\n");
        }

        System.out.println(sb);
    }

    private static String changeFirstCharacterCase(String str, boolean capitalize) {
        if (str != null && str.length() != 0) {
            StringBuilder sb = new StringBuilder(str.length());
            if (capitalize) {
                sb.append(Character.toUpperCase(str.charAt(0)));
            } else {
                sb.append(Character.toLowerCase(str.charAt(0)));
            }

            sb.append(str.substring(1));
            return sb.toString();
        } else {
            return str;
        }
    }

    public static String camelhumpToUnderline(String str) {
        Matcher matcher = Pattern.compile("[A-Z]").matcher(str);
        StringBuilder builder = new StringBuilder(str);

        for(int i = 0; matcher.find(); ++i) {
            builder.replace(matcher.start() + i, matcher.end() + i, "_" + matcher.group().toLowerCase());
        }

        if (builder.charAt(0) == '_') {
            builder.deleteCharAt(0);
        }

        return builder.toString();
    }

    public static String underlineToCamelhump(String str) {
        Matcher matcher = Pattern.compile("_[a-z]").matcher(str);
        StringBuilder builder = new StringBuilder(str);

        for(int i = 0; matcher.find(); ++i) {
            builder.replace(matcher.start() - i, matcher.end() - i, matcher.group().substring(1).toUpperCase());
        }

        if (Character.isUpperCase(builder.charAt(0))) {
            builder.replace(0, 1, String.valueOf(Character.toLowerCase(builder.charAt(0))));
        }

        return builder.toString();
    }

    public static String unique(String p1, String p2, String separator) {
        List<String> list1 = null;
        List<String> list2 = null;
        if (isNotBlank(p1)) {
            list1 = Arrays.asList(p1.split(separator));
        } else {
            list1 = Lists.newArrayList();
        }

        if (isNotBlank(p2)) {
            list2 = Arrays.asList(p2.split(separator));
        } else {
            list2 = Lists.newArrayList();
        }

        List<String> resultList = Collections.union((Collection)list1, (Collection)list2);
        Set<String> set = Sets.newHashSet(resultList);
        resultList.clear();
        resultList.addAll(set);
        return StringUtils.join(resultList, separator);
    }

    public static String lastStringOfSeparator(String params, String separator) {
        String[] paramArray = params.split(separator);
        String result = paramArray[paramArray.length - 1];
        return result;
    }

    public static <T> String join(T... elements) {
        return StringUtils.join(elements);
    }

    public static String get(String str, String defaults) {
        return isNullOrEmpty(str) ? defaults : str;
    }

    public static List<String> getTextImageSrc(String text) {
        if (isNullOrEmpty(text)) {
            return null;
        } else {
            String regex = "<\\s*[I|i][m|M][g|G]\\s+([^>]*)\\s*>";
            Pattern pa = Pattern.compile(regex, 32);
            Matcher ma = pa.matcher(text);
            ArrayList list = new ArrayList();

            while(ma.find()) {
                list.add(ma.group());
            }

            if (list.size() != 0) {
                List<String> imgSrcList = null;
                String a = null;
                Iterator var7 = list.iterator();

                while(var7.hasNext()) {
                    String s = (String)var7.next();
                    ma = Pattern.compile("[s|S][R|r][c|C]=[\"|'](.*?)[\"|']").matcher(s);
                    if (ma.find()) {
                        a = ma.group();
                        if (imgSrcList == null) {
                            imgSrcList = new ArrayList();
                        }
                    } else {
                        a = null;
                    }

                    if (a != null) {
                        a = a.replaceAll("[s|S][R|r][c|C]=[\"|']", "").replaceAll("[\"|']", "");
                        imgSrcList.add(a);
                    }
                }

                if (imgSrcList != null && imgSrcList.size() != 0) {
                    return imgSrcList;
                } else {
                    return null;
                }
            } else {
                return null;
            }
        }
    }

    public static String format(String format, Object... args) {
        return String.format(format, args);
    }

    public static String[] toArray(String str, String prefix) {
        return str.split(prefix);
    }

    public static List<String> toList(String str, String prefix) {
        String[] strArr = toArray(str, prefix);
        List<String> list = new ArrayList();
        if (str.length() > 0) {
            for(int i = 0; i < strArr.length; ++i) {
                list.add(strArr[i].toLowerCase());
            }
        }

        return list;
    }

    public static Map<String, String> toQueryMap(String query) {
        Map<String, String> result = Maps.newHashMap();
        String[] pairs = query.split("&");
        if (pairs != null && pairs.length > 0) {
            String[] var3 = pairs;
            int var4 = pairs.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                String pair = var3[var5];
                String[] param = pair.split("=", 2);
                if (param != null && param.length == 2) {
                    result.put(param[0], param[1]);
                }
            }
        }

        return result;
    }

    public static String toUnicode(String gbString) {
        char[] utfBytes = gbString.toCharArray();
        String unicodeBytes = "";

        for(int byteIndex = 0; byteIndex < utfBytes.length; ++byteIndex) {
            String hexB = Integer.toHexString(utfBytes[byteIndex]);
            if (hexB.length() <= 2) {
                hexB = "00" + hexB;
            }

            unicodeBytes = unicodeBytes + "\\u" + hexB;
        }

        return unicodeBytes;
    }

    public static String toUrl(String s) {
        return Encodes.encodeUrl(s);
    }

    public static String toUrlWithGB2312(String s) {
        return Encodes.encodeUrl(s, "gb2312");
    }

    private static byte charToByte(char c) {
        return (byte)"0123456789ABCDEF".indexOf(c);
    }

    public static byte[] toBytes(String str) {
        return str == null ? null : str.getBytes(Charsets.UTF_8);
    }

    public static byte[] toByteFromHexString(String hexString) {
        if (hexString != null && !hexString.equals("")) {
            hexString = hexString.toUpperCase();
            int length = hexString.length() / 2;
            char[] hexChars = hexString.toCharArray();
            byte[] d = new byte[length];

            for(int i = 0; i < length; ++i) {
                int pos = i * 2;
                d[i] = (byte)(charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
            }

            return d;
        } else {
            return null;
        }
    }

    public static String toStringCall(Object t) {
        StandardToStringStyle style = new StandardToStringStyle();
        style.setUseClassName(false);
        style.setUseIdentityHashCode(false);
        style.setContentStart("{");
        style.setContentEnd("}");
        style.setArrayStart("[");
        style.setArrayEnd("]");
        style.setArraySeparator(",");
        style.setFieldNameValueSeparator("=");
        style.setNullText("null");
        ToStringBuilder builder = new ReflectionToStringBuilder(t, style);
        return builder.toString();
    }

    public static String toString(Object obj) {
        return toString(obj, false);
    }

    public static String toString(Object obj, boolean prettyFormat) {
        StringBuffer s = new StringBuffer();
        if (obj instanceof LocalDate) {
            s.append(toString(((LocalDate)obj).toDate()));
        } else if (obj instanceof LocalTime) {
            s.append(toString(((LocalTime)obj).toString()));
        } else if (obj instanceof DateTime) {
            s.append(toString(((DateTime)obj).toDate()));
        } else if (obj instanceof LocalDateTime) {
            s.append(toString(((LocalDateTime)obj).toDate()));
        } else if (obj instanceof List) {
            List<?> obj_list = (List)obj;
            s.append('[');

            for(int i = 0; i < obj_list.size(); ++i) {
                s.append(toString(obj_list.get(i), prettyFormat));
                if (i < obj_list.size() - 1) {
                    s.append(',');
                }
            }

            s.append(']');
        } else if (obj instanceof Map) {
            Map<?, ?> obj_map = (Map)obj;
            List<?> obj_map_keys = Lists.newArrayList(obj_map.keySet());
            s.append('{');

            for(int i = 0; i < obj_map_keys.size(); ++i) {
                s.append("'" + obj_map_keys.get(i) + "'" + ":" + toString(obj_map.get(obj_map_keys.get(i)), prettyFormat));
                if (i < obj_map_keys.size() - 1) {
                    s.append(',');
                }
            }

            s.append('}');
        } else if (obj instanceof Object[]) {
            s.append(toString(Lists.newArrayList((Object[])((Object[])obj)), prettyFormat));
        } else {
            s.append(Jsons.toJSONString(obj, prettyFormat));
        }

        return s.toString();
    }

    public static String toUnderlineName(String s) {
        if (s == null) {
            return null;
        } else {
            StringBuilder sb = new StringBuilder();
            boolean upperCase = false;

            for(int i = 0; i < s.length(); ++i) {
                char c = s.charAt(i);
                boolean nextUpperCase = true;
                if (i < s.length() - 1) {
                    nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
                }

                if (i >= 0 && Character.isUpperCase(c)) {
                    if ((!upperCase || !nextUpperCase) && i > 0) {
                        sb.append('_');
                    }

                    upperCase = true;
                } else {
                    upperCase = false;
                }

                sb.append(Character.toLowerCase(c));
            }

            return sb.toString();
        }
    }

    public static String toCamelCase(String s) {
        if (s == null) {
            return null;
        } else {
            s = s.toLowerCase();
            StringBuilder sb = new StringBuilder(s.length());
            boolean upperCase = false;

            for(int i = 0; i < s.length(); ++i) {
                char c = s.charAt(i);
                if (c == '_') {
                    upperCase = true;
                } else if (upperCase) {
                    sb.append(Character.toUpperCase(c));
                    upperCase = false;
                } else {
                    sb.append(c);
                }
            }

            return sb.toString();
        }
    }

    public static String toCapitalizeCamelCase(String s) {
        if (s == null) {
            return null;
        } else {
            s = toCamelCase(s);
            return s.substring(0, 1).toUpperCase() + s.substring(1);
        }
    }

    public static String firstCharToUpperCase(String str) {
        return str.replaceFirst(str.substring(0, 1), str.substring(0, 1).toUpperCase());
    }
}
